package top.zhangjianyong.tools.service.mybatis.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.zhangjianyong.tools.entity.RemindItem;
import top.zhangjianyong.tools.mapper.RemindItemMapper;
import top.zhangjianyong.tools.service.mybatis.IRemindItemService;
import top.zhangjianyong.tools.utils.UUIDUtil;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;

/**
 * @author ： zhangjianyong
 * @date ： 2021/11/1 下午7:23
 */
@Service
public class IRemindItemServiceImpl extends ServiceImpl<RemindItemMapper, RemindItem> implements IRemindItemService {

    @Override
    public List<RemindItem> listByState(String state) {
        return lambdaQuery()
                .eq(RemindItem::getState, state)
                .eq(RemindItem::getDelFlag, 0).list();
    }

    @Override
    public Boolean saveErrorMessage(Long id, String message) {
        return lambdaUpdate()
                .eq(RemindItem::getId, id)
                .set(RemindItem::getErrorMessage, message)
                .update();
    }

    @Override
    public void close(Long id, String uuid) throws Exception {
        RemindItem remindItem = getById(id);
        if (remindItem == null) throw new Exception("未找到提醒事项");
        if (StringUtils.isBlank(uuid)) throw new Exception("uuid 为空");
        if (!uuid.equals(remindItem.getUuid())) throw new Exception("链接失效");

        String remindType = remindItem.getRemindType();
        if ("循环间隔".equals(remindType)) {
            Integer interval = remindItem.getIntervalTime();
            LocalDateTime dateTime = remindItem.getRemindTime().plusMinutes(interval);
            remindItem.setRemindTime(dateTime);
            remindItem.setUuid(UUIDUtil.uuid());
            updateById(remindItem);

        } else {
            lambdaUpdate().eq(RemindItem::getId, id)
                    .set(RemindItem::getState, "已确认").update();
        }
    }

    @Override
    public void close(Long id) throws Exception {
        RemindItem remindItem = getById(id);
        if (remindItem == null) throw new Exception("未找到提醒事项");
        if (remindItem.getRemindTime().isAfter(LocalDateTime.now())) {
            throw new Exception("未到达提醒时间");
        }

        String remindType = remindItem.getRemindType();
        if ("循环间隔".equals(remindType)) {
            Integer interval = remindItem.getIntervalTime();
            LocalDateTime dateTime = remindItem.getRemindTime().plusMinutes(interval);
            remindItem.setRemindTime(dateTime);
            remindItem.setUuid(UUIDUtil.uuid());
            updateById(remindItem);
        } else {
            lambdaUpdate().eq(RemindItem::getId, id)
                    .set(RemindItem::getState, "已确认").update();
        }
    }

    @Transactional
    @Override
    public void reset(String type) {
        List<RemindItem> list = lambdaQuery()
                .eq(RemindItem::getDelFlag, 0)
                .eq(RemindItem::getRemindType, type)
                .list();
        list.forEach(remindItem -> {
            LocalTime time = remindItem.getRemindTime().toLocalTime();

            remindItem.setRemindTime(LocalDateTime.now().with(time));
            remindItem.setState("未确认");
            remindItem.setUuid(UUIDUtil.uuid());
            updateById(remindItem);
        });

    }


}
